package com.llh.cheap.configs;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import com.qxt.file.services.AliOssFileService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author David
 */
@Configuration
@ComponentScan(basePackages = "com.qxt.file", excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {AliOssFileService.class}))
@Import({
        DatabaseConfig.class
})
@PropertySource("classpath:app.properties")
public class FileConfig {


    @Bean
    public COSClient cosClient(
            @Value("${file.cos.secret_id}") String secretId,
            @Value("${file.cos.secret_key}") String secretKey

    ) {
        // 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey, sessionToken)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region("ap-chengdu");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }

    @Bean("FILE_TT")
    public TransactionTemplate transactionTemplate(@Qualifier("MAIN_TT") TransactionTemplate transactionTemplate) {
        return transactionTemplate;
    }

    @Bean("FILE_DB")
    public JdbcTemplate db(@Qualifier("MAIN_DB") JdbcTemplate jdbcTemplate) {
        return jdbcTemplate;
    }

    @Bean("FILE_NAMED_DB")
    public NamedParameterJdbcTemplate namedDB(@Qualifier("MAIN_NAMED_DB") NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        return namedParameterJdbcTemplate;
    }

}
